package com.tnstart.db
{

	import com.tnstart.vo.Area;
	import com.tnstart.vo.Config;
	import com.tnstart.vo.Item;
	import com.tnstart.vo.ItemType;
	
	import flash.events.EventDispatcher;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	public class DBManager extends EventDispatcher
	{
		
		import flash.events.Event;
		import flash.data.SQLConnection;
		import flash.errors.SQLError;
		import flash.filesystem.File;

		private var _demoMode:Boolean = false;
		
		
		private var _demoFile:File;
		private var _dbFile:File;
		private var _conn:SQLConnection = new SQLConnection();
		private var _itemDAO:ItemDAO = null;
		private var _areaDAO:AreaDAO = null;
		private var _itemTypeDAO:ItemTypeDAO = null;
		private var _configDAO:ConfigDAO = null;

		
		
		[Event(name="loaded", type="flash.events.Event")]
		
		
		public function DBManager(dbFile:File) 
		{
			var preload:Boolean = false;
			
			_dbFile = dbFile;
			/*_demoFile = _dbFile.parent.resolvePath('finickyDemo.db');
			
			if (_demoFile.exists){
				_dbFile = _demoFile;
				_demoMode = true;
			}*/
			
			if (!_dbFile.exists){
				preload = true;
			}
			
			
			try
			{
				_conn.open(_dbFile);
				_itemDAO = new ItemDAO(_conn);
				_areaDAO = new AreaDAO(_conn);
				_itemTypeDAO = new ItemTypeDAO(_conn);
				_configDAO = new ConfigDAO(_conn);

				
				if (preload){
					loadDemoData();
				}
				onLoadComplete();
				
				
			}
			catch (error:SQLError)
			{
				trace("Error opening database");
				trace("error.message:", error.message);
				trace("error.details:", error.details);
				return;
			}
		}
		
		
		[Bindable]
		public function get demoMode():Boolean
		{
			return _demoMode;
		}

		public function set demoMode(value:Boolean):void
		{
			_demoMode = value;
			
			if (value){
				startDemo();
			}
			else{
				stopDemo();
			}
			
		}

		public function startDemo():void{
			_conn = new SQLConnection();
			_conn.open(_demoFile);
			_itemDAO = new ItemDAO(_conn);
			_configDAO = new ConfigDAO(_conn);
			loadDemoData();
			onLoadComplete();
		}
		
		private function loadDemoData():void{
			var id:String;
			
			var itemXML:XML = convertFileToXML(File.applicationDirectory.resolvePath("assets/demo/items.xml"));
			var configXML:XML = convertFileToXML(File.applicationDirectory.resolvePath("assets/demo/config.xml"));
			var areaXML:XML = convertFileToXML(File.applicationDirectory.resolvePath("assets/demo/areas.xml"));
			var itemTypeXML:XML = convertFileToXML(File.applicationDirectory.resolvePath("assets/demo/itemtypes.xml"));
			
			_conn.begin();
			
			for (id in itemXML.item){
				var item:Item = new Item();
				item.name = itemXML.item.name[id];
				item.type = itemXML.item.type[id];
				item.areaid = itemXML.item.areaid[id];
				item.iconpath = itemXML.item.iconpath[id];
				item.description =itemXML.item.description[id];
				_itemDAO.save(item);
			}
			
			for (id in configXML.config){
				var config:Config = new Config();
				config.host = configXML.config.host[id];
				config.port = configXML.config.port[id];
				config.apptitle = configXML.config.apptitle[id];
				_configDAO.save(config);
			}
			
			for (id in areaXML.area){
				var area:Area = new Area();
				area.name = areaXML.area.name[id];
				area.type = areaXML.area.type[id];
				_areaDAO.save(area);
			}
			
			for (id in itemTypeXML.itemtype){
				var itemType:ItemType = new ItemType();
				itemType.name = itemTypeXML.itemtype.name[id];
				itemType.type = itemTypeXML.itemtype.type[id];
				_itemTypeDAO.save(itemType);
			}
						
			_conn.commit();
			
		}
		
		private function convertFileToXML(xmlFile:File):XML{
			var fs:FileStream = new FileStream();
			fs.open(xmlFile, FileMode.READ);
			var results:XML = XML(fs.readUTFBytes(fs.bytesAvailable));
			fs.close();
			return results;
		}
		
		
		
		public function stopDemo():void{
			_conn = new SQLConnection();
			_conn.open(_dbFile);
			_itemDAO = new ItemDAO(_conn);
		
			
			try
			{
				_demoFile.deleteFile();
			} 
			catch(error:Error) 
			{
				trace(error.message);
			}
			
			onLoadComplete();
		}
		
		
		
		public function get itemDAO():ItemDAO
		{
			return _itemDAO;
		}
		
		public function set itemDAO(value:ItemDAO):void
		{
			_itemDAO = value;
		}
		
		public function onLoadComplete():void {
			dispatchEvent(new Event("loaded",true));
		}

		public function get configDAO():ConfigDAO
		{
			return _configDAO;
		}

		public function set configDAO(value:ConfigDAO):void
		{
			_configDAO = value;
		}

		public function get areaDAO():AreaDAO
		{
			return _areaDAO;
		}

		public function set areaDAO(value:AreaDAO):void
		{
			_areaDAO = value;
		}

		public function get itemTypeDAO():ItemTypeDAO
		{
			return _itemTypeDAO;
		}

		public function set itemTypeDAO(value:ItemTypeDAO):void
		{
			_itemTypeDAO = value;
		}
		
		
		
	}
}